<template>
  <div>
    <div class="addPermissTool" >
      <el-input size="small" placeholder="请输入角色英文名.." v-model="role.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input class="addPermissInput" size="small" v-model="role.nameZh" placeholder="请输入角色中文名.."></el-input>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="addRole">添加</el-button>
    </div>
    <div>
      <el-collapse v-model="activeName" accordion class="permissCollapse" @change="changeCollapse">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card >
            <div slot="header" class="clearfix">
              <span>可访问的资源</span>
              <el-button style="float: right; padding: 3px 0; color: red" type="text" icon="el-icon-delete" @click="delRole(r)" ></el-button>
            </div>
            <div>
              <el-tree
                  show-checkbox
                  :data="allMenus"
                  :props="defaultProps"
                  ref="tree"
                  :default-checked-keys="selectChecked"
                  node-key="id">
<!--                //绑定返回的id-->
              </el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button type="primary" plain @click="doUpdate(r.id,index)">修改</el-button>
                <el-button plain @click="noUpdate(r.id)">取消</el-button>
              </div>

            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissMana",
  data(){
    return{
      activeName:-1,
      role:{
        name:'',
        nameZh:''
      },
      roles:[],
      allMenus:[],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectChecked:[]
    }
  },
  mounted() {
    this.initPermiss();
  },
  methods:{
    delRole(role){
      this.$confirm('此操作将删除角色 ：[ ' + role.nameZh + ' ] 数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/api/system/basic/permiss/role/'+role.id).then(resp =>{
          this.initPermiss();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    addRole(){
      this.postRequest('/api/system/basic/permiss/',this.role).then(resp =>{
        if (resp){
          this.initPermiss();
          this.role.name = '';
          this.role.nameZh = '';
        }
      })
    },
    noUpdate(rid){
      this.changeCollapse(rid);
    },
    doUpdate(rid,index){
      let tree = this.$refs.tree[index];
      let selectedKey = tree.getCheckedKeys(true);
      let url = '/api/system/basic/permiss/?rid='+rid;
      selectedKey.forEach(key => {
        url = url + "&mids="+key;
      })
      this.putRequest(url).then(resp => {
        if (resp){
          this.initPermiss();
          this.activeName = -1;
        }
      })

    },
    initSelect(rid){
      this.getRequest('/api/system/basic/permiss/mid/'+rid).then(resp =>{
        this.selectChecked = resp;
      })
    },
    changeCollapse(rid){
      if (rid){
        this.initMenus();
        this.initSelect(rid);
      }
    },
    initMenus(){
      this.getRequest('/api/system/basic/permiss/menu').then(resp =>{
        if (resp){
          this.allMenus = resp;
        }
      })
    },
    initPermiss(){
      this.getRequest('/api/system/basic/permiss/').then(resp =>{
        if (resp){
          this.roles = resp;
        }
      })
    }
  }

}
</script>

<style scoped>
  .addPermissTool{
    display: flex;
    width: 700px;
  }
  .addPermissTool .addPermissInput{
    margin: 0 8px;
  }
  .permissCollapse{
    width: 700px;
  }
  .clearfix{

  }

</style>